package org.common4j2ee.stock.web.user;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.common4j2ee.stock.entity.user.Authority;
import org.common4j2ee.stock.entity.user.Resources;
import org.common4j2ee.stock.dao.utils.HibernateUtils;
import org.common4j2ee.stock.dao.utils.Page;
import org.common4j2ee.stock.dao.utils.PropertyFilter;
import org.common4j2ee.stock.service.user.AuthorityManager;
import org.common4j2ee.stock.service.user.ResourcesManager;
import org.common4j2ee.stock.service.utils.ServiceException;
import org.common4j2ee.stock.web.utils.CrudActionSupport;
import org.common4j2ee.stock.web.utils.PropertyFilterWeb;
import org.common4j2ee.stock.web.utils.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;
@Namespace("/user")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "authority!list.action", type = "redirect") })
public class AuthorityAction extends CrudActionSupport<Authority> {

	private Authority authority;

	private Long id;
	
	private List<Long> checkedResourceIds;
	
	public List<Long> getCheckedResourceIds() {
		return checkedResourceIds;
	}

	public void setCheckedResourceIds(List<Long> checkedResourceIds) {
		this.checkedResourceIds = checkedResourceIds;
	}

	private Page<Authority> page = new Page<Authority>(10);// 每页5条记录

	@Autowired
	AuthorityManager authorityManager;

	@Autowired
	ResourcesManager resourcesManager;
	
	private static final long serialVersionUID = 633445360169800367L;

	// -- CRUD Action 函数 --//
	public String list() throws Exception {
		 List<PropertyFilter> filters =
		 PropertyFilterWeb.buildFromHttpRequest(Struts2Utils.getRequest());
		 //设置默认排序方式
		 if (!page.isOrderBySetted()) {
		 page.setOrderBy("id");
		 page.setOrder(Page.ASC);
		 }
		 page = authorityManager.searchAuthority(page, filters);
		return SUCCESS;
	}
	
	public List<Resources> getAllResourceList() {

		return resourcesManager.getAllResourceList();
	}
	
	public Authority getauthority() {
		return authority;
	}

	public void setauthority(Authority authority) {
		this.authority = authority;
	}

	@Override
	public Authority getModel() {
		return authority;
	}

	@Override
	public String input() throws Exception {
		checkedResourceIds = authority.getResourceIdList();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		 HibernateUtils.mergeByCheckedIds(authority.getResources(),checkedResourceIds, Resources.class);
		 authorityManager.saveAuthority(authority);
		 addActionMessage("保存用户成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			 authorityManager.deleteAuthority(id);
			 addActionMessage("删除用户成功");
		} catch (ServiceException e) {
			 logger.error(e.getMessage(), e);
			 addActionMessage("删除用户失败");
		}
		return RELOAD;
	}

	// -- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkResourceName() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newLoginName = request.getParameter("name");
		String oldLoginName = request.getParameter("oldRoleName");
		if (authorityManager.isRoleNameUnique(newLoginName, oldLoginName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		 //因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id == null) {
			authority = new Authority();
		} else {
			authority = authorityManager.getAuthority(id);
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<Authority> getPage() {
		return page;
	}

	public void setPage(Page<Authority> page) {
		this.page = page;
	}

}
